<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>		
		<script>
	function Promise(executor){
		var _this = this;
		this.state = "pending";
		this.value = null;
		this.reson = null;
		this.onFulfilled = [];//成功的回调
		this.onRejected = []; //失败的回调		
		this.thenCallback = undefined;
		function resolve(value){
			if(_this.state==="pending"){
				_this.state = "fulfilled"
				_this.value = value;
				_this.onFulfilled.forEach(fn=>fn(value))
			}
		}
		function reject(reason){
			if(_this.state==="pending"){
				_this.state = "rejected"
				_this.reason = reason;
				_this.onRejected.forEach(fn => fn(reason))
			}
		}
		executor(resolve,reject);
	}
	Promise.prototype.then = function (onFulfilled,onRejected){
		if(this.state=="fulfilled"){
			typeof onFulfilled ==="function"&&onFulfilled(value);
		}
		if(this.state === "rejected"){
		        typeof onRejected === 'function' && onRejected(this.reason)
		}
		if(this.state==="pending"){
			typeof onFulfilled === 'function' && this.onFulfilled.push(onFulfilled)
			typeof onRejected === 'function' && this.onRejected.push(onRejected)
		}
		
	}
	var p = new Promise((resolve,reject)=>{
		setTimeout(()=>resolve("love"),5000);
	})
	p.then(res=>{
		console.log(res);
		return new Promise((resolve,reject)=>{
			setTimeout(function(){
				resolve(50)
			},3000)
		})
	})
	
	
	
		
		</script>
	</body>
</html>
